//是否处于小屏模式
var small_size = false

//判断距离可视上边距离
//移动当前组件至顶
function to_web_top(element, top) {
    var OffsetTop = element.offset().top;

    if (top !== undefined)
        return OffsetTop - top - 99
    else {
        $('html, body').animate({
            scrollTop: OffsetTop - 99
        }, 300);

        if (small_size)
            element.children("a").click()
    }
}

//切换 小屏、大屏模式
function mod_setter() {
    if (($(window).outerWidth() <= 768) != small_size) {
        small_size = $(window).outerWidth() <= 768
        if (small_size) {
            $(".help-cell-list").each(function (e) {
                $(this).attr("id", $(this).attr("fake-id"))
                $(this).removeAttr("fake-id")
            })
            $(".help-cell-title").addClass("alert").addClass("alert-info").attr("role", "alert")
            $(".main-content").parent("div").removeClass("col-10")
            $(".main-content").parent("div").addClass("col-12")
            $(".catalog").parent("div").css("display", "none")
            $(".help-cell-list").removeClass("show")
        } else {
            $(".help-cell-list").each(function (e) {
                $(this).attr("fake-id", $(this).attr("id"))
                $(this).removeAttr("id")
            })
            $(".help-cell-title").removeClass("alert").removeClass("alert-info").removeAttr("role", "alert")
            $(".main-content").parent("div").removeClass("col-12")
            $(".main-content").parent("div").addClass("col-10")
            $(".catalog").parent("div").removeAttr("style")
            $(".help-cell-list").addClass("show")
        }

        window.onscroll()
    }
}

$(function () {
    //页面滚动侦听
    window.onscroll = function (e) {
        var catalog_bottom_def = $(".catalog").parent("div").innerHeight() - $(".catalog").outerHeight()
        var top = $(this).scrollTop()
        var bottom = catalog_bottom_def - (top - 380)

        if (top < 351) {
            if ($(".catalog").css("position").trim() !== "static")
                $(".catalog").removeAttr("style").css("position", "static")
        } else if (bottom <= 30) {
            if ($(".catalog").css("position").trim() !== "absolute")
                $(".catalog").removeAttr("style").css("position", "absolute ").css("bottom", 0)
        } else if ($(".catalog").css("position").trim() !== "fixed")
            $(".catalog").removeAttr("style").css("position", "fixed").css("top", 150)

        var now = 0
        $(".main-content h1, .main-content h2, .main-content h3, .main-content h4, .main-content h5, .main-content h6").each(function (e) {
            var totop = to_web_top($(this), top)
            if (totop > 1)
                return false
            now++
        })

        now = (now == 0 ? 1 : now)

        $(".catalog li a").removeAttr("style")
        $(".catalog li a").eq(now - 1).css("color", "#ff6a00")
    }

    //侦听宽度变化
    $(window).resize(mod_setter);

    //自动生成目录
    var catalog_html = "";
    $(".main-content h1, .main-content h2, .main-content h3, .main-content h4, .main-content h5, .main-content h6").each(function (e) {
        catalog_html += '<li><a href="javascript:void(0);">' + $(this).text() + '</a></li>'
    })

    $(".catalog ul").html(catalog_html)
    $(".catalog li a").eq(0).css("color", "#ff6a00")

    //目录按键侦听
    $(".catalog li a").click(function () {
        var index = $(".catalog li a").index(this)
        to_web_top($(".main-content h1, .main-content h2, .main-content h3, .main-content h4, .main-content h5, .main-content h6").eq(index))
    })

    //初始化宽度设置
    mod_setter()

    //定位
    if ($.getUrlParam("pos") != null) {
        to_web_top($(".main-content h1, .main-content h2, .main-content h3, .main-content h4, .main-content h5, .main-content h6").eq($.getUrlParam("pos") - 1))
    }
})